package com.xf.controller;

import com.xf.common.core.domain.DmpResult;
import com.xf.common.core.entity.QueryRequest;
import com.xf.common.core.entity.dmp.DmpOrganization;
import com.xf.common.core.exception.FebsException;
import com.xf.common.core.utils.FebsUtil;
import com.xf.service.OrganizationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 组织
 *
 * @author xufeng
 * @version 1.0
 * @date 2021/10/20
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("workspace")
public class WorkspaceController {
    @Autowired
    private OrganizationService tenantService;


    @GetMapping
    public DmpResult list(QueryRequest request, DmpOrganization organization) {
        Map<String, Object> dataTable = FebsUtil.getDataTable(tenantService.getPage(request, organization));
        return DmpResult.data(dataTable);
    }

    /**
     * 创建工作空间
     *
     * @param tenant
     * @return
     */
    @PostMapping("/create")
    public DmpResult createWorkspace(@Valid DmpOrganization tenant) {
        tenantService.create(tenant);
        return DmpResult.success();

    }

    /**
     * 修改工作空间信息
     *
     * @param tenant
     * @return
     */
    @PutMapping("/update")
    public DmpResult updateWorkspace(@Valid DmpOrganization tenant) {
        //验证开始
        if (!"admin".equals(FebsUtil.getCurrentUsername())) {
            throw new FebsException("非admin用户无法修改工作空间");
        }
        boolean flag = tenantService.updateById(tenant);
        return DmpResult.success();

    }

    /**
     * 删除工作空间信息
     *
     * @param tenantId 工作空间id
     * @return
     */
    @DeleteMapping("/del")
    public DmpResult updateWorkspace(@RequestParam("tenantId") String tenantId) {

        boolean flag = tenantService.removeById(tenantId);
        return DmpResult.success();

    }

}
